/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.EstPriceBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.EstPriceDao;
import com.minierp.dao.IdGenerator;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.EstPrice;
import com.minierp.model.MakeType;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class EstPriceFormCtr extends SimpleFormController {
	private EstPriceDao estPriceDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		EstPriceBean bean = new EstPriceBean();
		if (id != null && !id.equals("")) {
			EstPrice estId1 = (EstPrice) this.estPriceDao.get(EstPrice.class,
					Long.parseLong(id));
			bean.setEstPrice(estId1);
			bean.setPriceStr(estId1.getPrice().toString());
			if (estId1.getMakeTypeId() != 0L) {
				MakeType priceType1 = (MakeType) this.estPriceDao.get(
						MakeType.class, estId1.getMakeTypeId());
				if (priceType1 != null) {
					bean.setMtName(priceType1.getTypeName());
				}
			}
		} else {
			String estId = request.getParameter("est_id");
			String priceType = request.getParameter("price_type");
			String estType = request.getParameter("est_type");
			new EstPrice();
			EstPrice form = new EstPrice();
			form.setEstType(estType);
			form.setPriceType(priceType);
			if (priceType != null && priceType.equals("EST")) {
				form.setEstId(Long.parseLong(estId));
			}

			bean.setEstPrice(form);
		}

		return bean;
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String id = request.getParameter("id");
		EstPriceBean bean = (EstPriceBean) command;
		EstPrice obj = bean.getEstPrice();
		String alertMsg = "";
		String priCode = "";
		if (obj.getEstType().equals("CLIENT")) {
			priCode = "CLIE_PRIC";
		} else if (obj.getEstType().equals("PROVIDER")) {
			priCode = "PROV_PRIC";
		} else if (obj.getEstType().equals("COOPERATOR")) {
			priCode = "COOP_PRIC";
		}

		if (priCode.equals("")) {
			throw new Exception("Est type not supported!");
		} else if (EncCommonHelper.checkPrivilege(request, priCode)
				&& !EncCommonHelper.checkPurePrivilege(request, "NO_PRIC_EDIT")) {
			obj.setProSpell(PageHelper.getPinYinHeadChar(obj.getProName())
					.toUpperCase());
			UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile multipartFile = multipartRequest
					.getFile("attach_file");
			String continueFlag;
			if (multipartFile != null && !multipartFile.isEmpty()) {
				String mv = multipartFile.getOriginalFilename();
				continueFlag = EncCommonHelper.getExtendName(mv);
				String uploadPath = EncCommonHelper.getUploadVirtualPath(this
						.getServletContext());
				String realPath = this.getServletContext().getRealPath(
						uploadPath);
				String fileName = ub.getUser().getId() + "_"
						+ System.currentTimeMillis() + "." + continueFlag;
				FileCopyUtils.copy(multipartFile.getBytes(), new File(realPath
						+ System.getProperty("file.separator") + fileName));
				obj.setAttachFile(uploadPath + fileName);
			} else if (bean.getAttachDelete().equals("Y")) {
				obj.setAttachFile("");
			}

			if (id != null && !id.equals("")) {
				this.estPriceDao.update(obj);
				alertMsg = "修改成功！";
			} else {
				obj.setId(this.idGenerator.getId(EstPrice.class.getName()));
				this.estPriceDao.save(obj);
				alertMsg = "增加成功！";
			}

			continueFlag = request.getParameter("continue_flag");
			ModelAndView mv1;
			if (continueFlag != null && continueFlag.equals("1")) {
				mv1 = new ModelAndView(
						"redirect:/base/EstPriceNewSimple.ihtm?est_id="
								+ obj.getEstId() + "&est_type="
								+ obj.getEstType() + "&price_type="
								+ obj.getPriceType());
			} else {
				mv1 = new ModelAndView(this.getSuccessView());
				mv1.addObject("returnValue", String.valueOf(obj.getId()));
			}

			EncCommonHelper.setAlertMessage(request, alertMsg);
			return mv1;
		} else {
			throw new Exception("Permission deny!");
		}
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public EstPriceDao getEstPriceDao() {
		return this.estPriceDao;
	}

	public void setEstPriceDao(EstPriceDao estPriceDao) {
		this.estPriceDao = estPriceDao;
	}
}